import re


class ProcessLog:
    def __init__(self, raw_text: str):
        """程序输出文本类"""
        self.__raw_text = raw_text.strip("\n").strip("\r")
        self.text = ""
        self.color_text = ""
        self.deal_log()

    def deal_log(self):
        """CQ码屏蔽与颜色处理"""
        self.__raw_text = re.sub("\\[CQ:.+?]", "", self.__raw_text)
        # 处理输出颜色：有颜色保持，无颜色增添
        if re.search("\033\\[.*?m", self.__raw_text):
            self.text = re.sub("\033\\[.*?m", "", self.__raw_text)
            self.color_text = self.__raw_text
        else:
            self.text = self.__raw_text
            # 为 WARN和 ERROR输出增添颜色
            if re.search("\\[.*INFO.*]", self.__raw_text):
                self.color_text = f"\033[0m{self.__raw_text}"
            elif re.search("\\[.*WARN.*]", self.__raw_text):
                self.color_text = f"\033[33m{self.__raw_text}"
            elif re.search("\\[.*ERR.*]", self.__raw_text):
                self.color_text = f"\033[31m{self.__raw_text}"
            # 中括号开头恢复颜色，其它保持
            elif re.match("\\[", self.__raw_text):
                self.color_text = f"\033[0m{self.__raw_text}"
            else:
                self.color_text = self.__raw_text


class RobotMsg:
    def __init__(self, text=""):
        """机器人消息类"""
        self.text = text
        self.length = len(self.text)

    def __add__(self, other):
        tmp = RobotMsg()
        if self.length:
            tmp.text = self.text + "\n" + other.text
        else:
            tmp.text = other.text
        tmp.length = self.length + other.length
        return tmp
